#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>

#include <stdlib.h>
#include <stdio.h>


Adafruit_SHT31 sht31 = Adafruit_SHT31();
TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h

/* 时钟显示配置 */
uint32_t targetTime = 0;       // for next 1 second timeout
byte omm = 99;
bool initial = 1;
byte xcolon = 0;
unsigned int colour = 0;
static uint8_t conv2d(const char* p) {
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}
uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6);  // Get H, M, S from compile time



void setup() {
  // put your setup code here, to run once:
  tft.init();
  tft.setRotation(5);
//  tft.fillScreen(TFT_BLACK);

  if (! sht31.begin(0x44)) {   // Set to 0x45 for alternate i2c addr
    while (1) delay(1);
  }
  
  targetTime = millis() + 1000; 
  for (int i = 0; i < 160; i++) {
      tft.drawFastVLine(i, 0, tft.height(), TFT_BLACK);
   }
} 

void loop() {
  // put your main code here, to run repeatedly:
  

//
  if (targetTime < millis()) {
    targetTime = millis() + 1000;
    ss++;              // Advance second
    if (ss==60) {
      ss=0;
      omm = mm;
      mm++;            // Advance minute
      if(mm>59) {
        mm=0;
        hh++;          // Advance hour
        if (hh>23) {
          hh=0;
        }
      }
    }

    if (ss==0 || initial) {
      initial = 0;
     }

    // Update digital time
    byte xpos = 6;
    byte ypos = 6;
    if (omm != mm) { // Only redraw every minute to minimise flicker
      // Uncomment ONE of the next 2 lines, using the ghost image demonstrates text overlay as time is drawn over it
      tft.setTextColor(TFT_BLACK, TFT_BLACK);  // Leave a 7 segment ghost image, comment out next line!
      //tft.setTextColor(TFT_BLACK, TFT_BLACK); // Set font colour to black to wipe image
      // Font 7 is to show a pseudo 7 segment display.
      // Font 7 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 0 : .
      tft.drawString("88:88",xpos,ypos,7); // Overwrite the text to clear it
      tft.setTextColor(0xFBE0); // Orange
      omm = mm;

      if (hh<10) xpos+= tft.drawChar('0',xpos,ypos,7);
      xpos+= tft.drawNumber(hh,xpos,ypos,7);
      xcolon=xpos;
      xpos+= tft.drawChar(':',xpos,ypos,7);
      if (mm<10) xpos+= tft.drawChar('0',xpos,ypos,7);
      tft.drawNumber(mm,xpos,ypos,7);
    }

    if (ss%2) { // Flash the colon
      tft.setTextColor(0x39C4, TFT_BLACK);
      xpos+= tft.drawChar(':',xcolon,ypos,7);
      tft.setTextColor(0xFBE0, TFT_BLACK);
      for (int i = 90; i < 160; i++) {
      tft.drawFastVLine(i, 60, tft.height(), TFT_BLACK);
   }

    /* 温度湿度显示模块 */
    float t = sht31.readTemperature();
    float h = sht31.readHumidity();

    tft.setTextColor(TFT_WHITE);
    
    tft.drawCentreString("Temp :", 40, 60, 4); // Draw text centre at position 80, 24 using font 4
    tft.drawCentreString("Humi :", 40, 90, 4); // Draw text centre at position 80, 24 using font 4

    tft.drawFloat(t, 2, 90, 60, 4);   // float数据，小数点位数，横轴坐标，纵轴坐标，字体大小。
    tft.drawFloat(h, 2, 90, 90, 4);
    }
    else {
      tft.drawChar(':',xcolon,ypos,7);
      
  }
  }
}
